package top.xzxsrq.fileviewutils.Scheduled;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import top.xzxsrq.fileviewutils.entity.DownloadFile;
import top.xzxsrq.fileviewutils.entity.ShareFile;
import top.xzxsrq.fileviewutils.entity.TokensAreDiscarded;
import top.xzxsrq.fileviewutils.mapper.DownloadFileMapper;
import top.xzxsrq.fileviewutils.mapper.ShareFileMapper;
import top.xzxsrq.fileviewutils.mapper.TokensAreDiscardedMapper;

import java.util.Date;

@Component
@Log4j2
@Transactional
public class ClearScheduled {
    @Autowired
    private TokensAreDiscardedMapper tokensAreDiscardedMapper;
    @Autowired
    private ShareFileMapper shareFileMapper;
    @Autowired
    private DownloadFileMapper downloadFileMapper;

    /**
     * 每分钟执行一次清除过期的token或者文件分享
     */
    @Scheduled(cron = "0 * * * * ? ")
    public void clear() {
        log.info("开始清理废弃token");
        LambdaQueryWrapper<TokensAreDiscarded> tokensAreDiscardedLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tokensAreDiscardedLambdaQueryWrapper.le(TokensAreDiscarded::getEntTime, new Date());
        tokensAreDiscardedMapper.delete(tokensAreDiscardedLambdaQueryWrapper);
        log.info("开始清理过期分享文件");
        LambdaQueryWrapper<ShareFile> shareFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shareFileLambdaQueryWrapper.le(ShareFile::getEntTime, new Date());
        shareFileMapper.delete(shareFileLambdaQueryWrapper);
        log.info("开始清理过期下载文件");
        LambdaQueryWrapper<DownloadFile> downloadFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        downloadFileLambdaQueryWrapper.le(DownloadFile::getEntTime, new Date());
        downloadFileMapper.delete(downloadFileLambdaQueryWrapper);
    }
}
